package com.tang.sharding.schedule.task.manager;

import com.tang.sharding.schedule.task.AbstractRunnableTask;
import com.tang.sharding.schedule.task.hook.CronTaskShutdownHook;
import org.springframework.scheduling.config.CronTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

/**
 * @author Tang
 * @classname CronTaskManager
 * @description [  ]
 * @date 2021/8/1 21:03
 */
public class CronTaskManager extends AbstractTaskManager {

    public CronTaskManager(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        super(scheduledTaskRegistrar);
        // 注册hook
        Runtime.getRuntime().addShutdownHook(new CronTaskShutdownHook(this));
    }

    /**
     * 添加任务
     *
     * @param runnableTask
     * @param expression
     */
    public void addTask(AbstractRunnableTask runnableTask, String expression) {
        CronTask cronTask = new CronTask(runnableTask, expression);
        super.addTask(runnableTask, cronTask);
    }

}